<template>
  <div class="container">
     <div class="login_box">
        <el-form class="login_form" :model="loginForm" :rules='loginFormRules' ref="loginFormRef">
          <!-- 用户名 -->
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user-solid" v-model="loginForm.username"></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" type='password' v-model="loginForm.password"></el-input>
          </el-form-item>
          <!-- 按钮区域 -->
          <el-form-item class="btns">
            <el-button type="primary" @click="login()" >登录</el-button>
            <el-button type="info" @click="resetLoginForm()">重置</el-button>
          </el-form-item>
        </el-form>
          <el-row>
            <el-col>
              <div class="userinfo">
                <h5 class="text">管理员权限：</h5>
                <span class="username">用户名：admin</span>
                <span class="password">密码：admin123456</span>
              </div>
            </el-col>
            <el-col>
              <div class="userinfo">
                <h5 class="text">普通用户权限：</h5>
                <span class="username">用户名：root1234</span>
                <span class="password">密码：root1234</span>
              </div>
            </el-col>
          </el-row>
     </div>
  </div>
</template>

<script>
// 导入网络请求
import { postlogin } from '../../network/login'
// vuex
import { mapActions } from 'vuex'
export default {
  name: 'login',
  data () {
    return {
      // 登录表单数据绑定
      loginForm: {
        username: 'admin',
        password: 'admin123456'
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入正确用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入正确密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    ...mapActions(['setUser']),
    login () {
      this.$refs.loginFormRef.validate(valid => {
        if (valid) {
          // this.$axios.post('http://localhost:3033/login', this.loginForm)
          postlogin(this.loginForm)
            .then(res => {
              console.log()
              if (res.status === 200) {
                this.$message.success('登录成功')
                // 把token放在sessionStorage中
                window.sessionStorage.setItem('token', res.data[1].token)
                this.setUser(res.data[1])
                this.$router.push('/home')
              } else {
                this.$message.error('登录失败，用户名或者密码错误')
              }
            })
        } else {
          this.$message.error('请输入账号或者密码')
        }
      })
    },
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields()
    }
  }
}
</script>

<style lang="stylus" scoped>
.container
  background-color #2b4b6b
  width 100%vw
  height 100%vh
  .login_box
    position absolute
    left 50%
    top 50%
    transform translate(-50%,-50%)
    width 360px
    height 280px
    background-color #fff
    border-radius 2px
    .login_form
      width 100%
      padding 30px 16px 8px 16px
      box-sizing border-box
      .btns
        display flex
        justify-content flex-end
        margin 0
    .userinfo
      width 100%
      padding 0 16px 5px 16px
      .text
        font-size 13px
        margin-bottom 3px
      .username
        padding-left 12px
        color #909399
      .password
        padding-left 12px
        color #909399
</style>
